package com.power.ding.dao.impl;

import com.plf.common.utils.StringUtil;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.ding.model.pageInfo.Di_rel_dept_correctPageInfo;
import com.power.ding.model.pojo.bo.Di_rel_dept_BaseUser;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;
import com.power.ding.dao.Di_rel_dept_correctDaoCustom;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 司法社矫组织机构表 持久层实现(hibernate)
 */

@Repository
public class Di_rel_dept_correctDaoImpl implements Di_rel_dept_correctDaoCustom {

    @Resource
    private EntityManager entityManager;

    @Override
    public List<Map<String, Object>> queryPageDeptAndUser(Di_rel_dept_correctPageInfo pageInfo) {
        StringBuilder sql = new StringBuilder("select d.id, orgname, fid, adid, contactuser, orgtype,u.username,u.mobile,u.careerranktext,u.sextext from di_rel_dept_correct d left join di_user u  on d.contactuser = u.id where 1=1 ");
        List params = new ArrayList();
        int index = 1;
        if (!StringUtil.isEmpty(pageInfo.getF_id())) {
            sql.append(" and d.id = ?" + index++);
            params.add(pageInfo.getF_id());
        }
        if (!StringUtil.isEmpty(pageInfo.getU_name())) {
            sql.append(" and u.username like ?" + index++);
            params.add("%" + pageInfo.getU_name() + "%");
        }
        if (!StringUtil.isEmpty(pageInfo.getF_orgname())) {
            sql.append(" and d.orgname like ?" + index++);
            params.add("%" + pageInfo.getF_orgname() + "%");
        }
        if (!StringUtil.isEmpty(pageInfo.getF_adid())) {
            sql.append(" and d.adid = ?" + index++);
            params.add(pageInfo.getF_adid());
        }
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);

        for (int i = 0; i <= params.size() - 1; i++) {
            query.setParameter(i + 1, params.get(i));
        }

        List<Map<String, Object>> result = query.getResultList();
        if (!CollectionUtils.isEmpty(result)) {
            pageInfo.setAllcount(result.size());
            int startIndex = (pageInfo.getPageNumber() - 1) * pageInfo.getPageSize();
            int endIndex = startIndex + pageInfo.getPageSize();
            if (result.size() >= endIndex) {
                result = result.subList(startIndex, endIndex);
            } else {
                result = result.subList(startIndex, result.size());
            }
        } else {
            pageInfo.setAllcount(0);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryBaseOrgUserByUser(Di_rel_dept_BaseUser baseUser) {
        StringBuilder sql = new StringBuilder("select userid, username, mobile, careerranktext, sextext, orgid, orgname from v_rel_dept_lawuser v where 1=1 ");
        List params = new ArrayList();
        int index = 1;
        if (!StringUtil.isEmpty(baseUser.getUsername())) {
            sql.append("and v.username like ?" + index++);
            params.add("%" + baseUser.getUsername() + "%");
        }
        if (!StringUtil.isEmpty(baseUser.getOrgid())) {
            sql.append(" and v.orgid = ?" + index++);
            params.add(baseUser.getOrgid());
        }
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);

        for (int i = 0; i <= params.size() - 1; i++) {
            query.setParameter(i + 1, params.get(i));
        }

        return query.getResultList();
    }
}